package com.yb.bussiness.goods;

import java.util.List;

import com.yb.base.dao.BaseDao;
import com.yb.base.dao.IBaseDao;
import com.yb.domain.goods.TBrandType;

public class TBrandTypeServiceImpl implements TBrandTypeService {

	private IBaseDao<TBrandType, Long> dao;

	public TBrandTypeServiceImpl() {
		dao = new BaseDao<TBrandType, Long>(TBrandType.class);
	}

	public void delete(TBrandType tbt) {
		dao.delete(tbt);
	}

	public void save(TBrandType tbt) {
		dao.saveEntity(tbt);
	}

	public List<TBrandType> findByBIdAndCid(Long Bid, Long cid) {
		String sql = "select * from t_brand_type where brandId= ? and categoryId = ?";
		return dao.findList(sql, new Object[] { Bid, cid });
	}

	public void deleteByBidAndCid(Long bid, Long cid) {
		String sql = "delete from t_brand_type where brandId= ? and categoryId = ?";
		dao.delete(sql, new Object[] { bid, cid });
	}

    public void delteBYBidAndCidAndTypeId(Long bid, Long cid, Long typeId) {
        String sql = "delete from t_brand_type where brandId= ? and categoryId = ? and typeId = ?";
        dao.delete(sql, new Object[] { bid, cid, typeId });
    }

	public boolean checkDel(Long bid, Long cid) {
		boolean flag = true;
		String sql = "select count(*) from t_pinggou where categoryId = ? and brandId = ?";
		if (bulidResult(sql, new Object[] { cid,bid }) > 0)
			flag = false;
		return flag;
	}

	private int bulidResult(String sql, Object[] conditionAry) {
		int count = 0;
		List<Object[]> list = dao.findObjectArryList(sql, conditionAry);
		Object[] objAry = list.get(0);
		count = new Integer(objAry[0].toString());
		return count;
	}

	public Long findCidByType(String typeId) {
		String sql = "select ifnull(max(categoryid),0) from t_brand_type where typeid='"+typeId+"'";
		List<Object[]> list = dao.findObjectArryList(sql);
		Object[] objAry = list.get(0);
		return Long.valueOf((objAry[0].toString()));
	}

}
